package framework;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.Timer;


/**
 * @author Michael Deng (modified)
 * @author Howard Chung (modified)
 * 
 */
@SuppressWarnings("serial")
public class GameContainer extends JFrame {

    private static final int ONE_SECOND = 1000;
    // private static final int FRAMES_PER_SECOND = 30;
    private Timer myTimer;
    private Game myGame;
    private String myLastKeyPressed;

    /**
     * Initializes the window and starts the animation --- called by main.
     * 
     * @param size the window
     * @param gamePackage name of the game's package
     */
    public void init (Dimension size, String gamePackage, int FRAMES_PER_SECOND) {
        myGame = new Game(this, gamePackage);
        myTimer = new Timer(ONE_SECOND / FRAMES_PER_SECOND, new ActionListener() {
            @Override
            public void actionPerformed (ActionEvent e) {
                myGame.update();
                repaint();
            }
        });
        start();
        setSize(size);
        setPreferredSize(size);
        setInputListeners();
        setFocusable(true);
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        requestFocus();
    }

    /**
     * Starts the applet's action, i.e., starts the animation.
     */
    public void start () {
        myTimer.start();
    }

    /**
     * Stops the applet's action, i.e., the animation.
     */
    public void stop () {
        myTimer.stop();
    }

    /**
     * Never called by you directly, instead called by Java runtime
     * when area of screen covered by this container needs to be
     * displayed (i.e., creation, uncovering, change in status)
     * 
     * @param pen smart pen to draw on the canvas with <- where is this
     *        parameter initialized?
     */
    @Override
    public void paint (Graphics pen) {
        pen.setColor(Color.BLACK);
        pen.fillRect(0, 0, getSize().width, getSize().height);
        myGame.paint((Graphics2D) pen);
    }

    /**
     * Returns the last key pressed by the player (or "" if none pressed).
     */
    public String getLastKeyPressed () {
        return myLastKeyPressed;
    }

    /**
     * Create listeners that will update state based on user input.
     */
    private void setInputListeners () {
        myLastKeyPressed = "";
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed (KeyEvent e) {
                myLastKeyPressed = KeyEvent.getKeyText(e.getKeyCode());
            }

            @Override
            public void keyReleased (KeyEvent e) {
                myLastKeyPressed = "";
            }
        });
    }

}
